// Test setup for all test suites
const fs = require('fs');
const path = require('path');

// Ensure test directories exist
const testDirs = ['unit', 'integration', 'mcp'];
testDirs.forEach(dir => {
  const dirPath = path.join(__dirname, dir);
  if (!fs.existsSync(dirPath)) {
    fs.mkdirSync(dirPath, { recursive: true });
  }
});

// Global test utilities
global.testUtils = {
  createTempConfig: (config) => {
    const tempDir = path.join(__dirname, '../temp');
    if (!fs.existsSync(tempDir)) {
      fs.mkdirSync(tempDir, { recursive: true });
    }
    const configPath = path.join(tempDir, 'test-config.json');
    fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
    return configPath;
  },

  cleanupTempConfig: (configPath) => {
    if (fs.existsSync(configPath)) {
      fs.unlinkSync(configPath);
    }
  },

  createTempLogDir: () => {
    const tempDir = path.join(__dirname, '../temp-logs');
    if (!fs.existsSync(tempDir)) {
      fs.mkdirSync(tempDir, { recursive: true });
    }
    return tempDir;
  }
};

// Test timeout handling
jest.setTimeout(120000); // 2 minutes for slow tests

// Cleanup after tests
afterEach(() => {
  // Clean up any test files created during tests
  const tempDir = path.join(__dirname, '../temp');
  if (fs.existsSync(tempDir)) {
    fs.rmSync(tempDir, { recursive: true, force: true });
  }
});